float4x4 World;
float4x4 View;
float4x4 Projection;

texture Texture;
sampler diffuseSampler = sampler_state
{
    Texture = (Texture);
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
    MIPFILTER = LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal   : NORMAL0;
    float2 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position         : POSITION0;
	float2 TexCoord : TEXCOORD0;
    float2 Depth            : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(float4(input.Position.xyz, 1), World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

	output.TexCoord = input.TexCoord;

    output.Depth.x = output.Position.z;
    output.Depth.y = output.Position.w;

    return output;
}

struct PixelShaderOutput
{
    float4 Depth     : COLOR0;
};

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
    PixelShaderOutput output = (PixelShaderOutput)0;

	//output.Depth = tex2D(diffuseSampler, input.TexCoord);
    //output.Depth.a = 1.0;

    // depth in screen space.
    output.Depth = input.Depth.x / input.Depth.y;

    return output;
}

technique Occlusion
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

struct VertexShaderPointOutput
{
    float4 Position         : POSITION0;
	float Distance          : TEXCOORD0;
    float2 TexCoord         : TEXCOORD1;
};

VertexShaderPointOutput VertexShaderPointFunction(VertexShaderInput input)
{
    VertexShaderPointOutput output;

    float4 worldPosition = mul(float4(input.Position.xyz, 1), World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    output.Distance = length(viewPosition.xyz);

    output.TexCoord = input.TexCoord;

    return output;
}

PixelShaderOutput PixelShaderPointFunction(VertexShaderPointOutput input)
{
    PixelShaderOutput output = (PixelShaderOutput)0;

    output.Depth = input.Distance;
    //output.Depth = tex2D(diffuseSampler, input.TexCoord);

    return output;
}

technique PointOcclusion
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderPointFunction();
        PixelShader = compile ps_2_0 PixelShaderPointFunction();
    }
}
